<template>
  <div>
    <el-card class="box-card">
      <div class="news_header">
        <MainSearchForm :formList="formList" :formValue="formValue" @formReset="formChange" @formSearch="formChange"></MainSearchForm>
        <!-- <el-button size="mini" type="primary" class="add-btn" @click="addNews">新增</el-button> -->
      </div>
      <div class="main_table mg-t-20" style="min-height: 600px">
        <MainTable
          ref="orderTable"
          :loading="listLoading"
          :columns="columns"
          :tableData="tableData"
          showSelection
          @selection-change="handleSelectionChange"
        >
          <template v-slot:status="{ row }">
            <span :style="{ color: row.status == 1 ? '#03A98E' : '#999999' }">{{ row.status == '1' ? '上线' : '下线' }}</span>
          </template>
          <el-table-column label="操作" width="200">
            <template slot-scope="scope">
              <el-link
                style="color: #42BEAA"
                size="mini"
                @click="editBanner(scope.row, 2)"
                title="编辑"
                >编辑</el-link
              >
              <el-link
                style="color: #444444"
                size="mini"
                @click="editBanner(scope.row, 1)"
                title="查看"
                >查看</el-link
              >
              <el-popconfirm
                confirm-button-text="是的"
                cancel-button-text="不用了"
                icon="el-icon-info"
                icon-color="red"
                title="确定删除吗？"
                @onConfirm="deleteNews(scope.row)"
              >
                <el-link
                  style="color: #F56C6C"
                  size="mini"
                  title="删除"
                  slot="reference"
                  >删除</el-link
                >
              </el-popconfirm>
            </template>
          </el-table-column>
        </MainTable>
      </div>

      <div class="pagination-right">
        <MainPagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageSize"
          layout=" ->,total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </MainPagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import { pinnedArticle } from "@/api/news/index";
import { getBannerList, deleteBannerList } from "@/api/news/banner";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import MainTable from '@/components/MainTable/table.vue'
import { indexData } from '../MockData'
const { columns, formList, formValue } = indexData
export default {
  name: "news",
  components: {
    MainTable
  },
  data() {
    return {
      handleScrool: "",
      formList,
      formValue,
      sendTime: [],
      tableData: [],
      currentPage: 1,
      pageSize: 20,
      total: 0,
      // 勾选的数据
      multipleSelection: [],
      // 新闻日期
      pickerOptions: [],
      listLoading: false,
      dialogData:{
        title:"",
        status:false,
        mydata:{}
      },
      columns
    };
  },
  activated() {
    // this.getData();
  },

  methods: {
    //  表单搜索
    formChange() {
      this.currentPage = 1
      this.pageSize = 20
      this.getData()
    },
    getData() {
      return 
      this.listLoading = true;
      let data = {
        page: this.currentPage,
        page_size: this.pageSize,
        ...this.formValue
      };
      let query = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, data)) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        data,
      );
      getBannerList(query).then(
        (res) => {
          this.listLoading = false;
          this.tableData = res.data.list;
          this.total = res.data.total;
        },
        (err) => {
          console.log(err);
        }
      );
    },
    //  置顶新闻
    pinnedNews(row) {
      pinnedArticle({ id: row.id }).then(
        (res) => {
          if (res.errcode == 0) {
            this.$message.success("置顶成功");
            this.getData();
          } else {
            this.$message.error(res.errmsg);
          }
        },
        (err) => {
          console.log(err);
        }
      )
    },
    addNews() {
      this.$router.push("/news/banneradd");
    },
    editBanner(row, pageControls) {
      // 修改新闻
      this.$router.push({
        path: "/news/banneradd",
        query: {
          id: row.id,
          pageControls
        }
      });
    },
    close(){
      this.dialogData.status=false;
      location.reload()
    },
    deleteNews(row) {
      // 删除新闻
      let query = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { id: row.id }
      );
      deleteBannerList(query).then(
        (res) => {
          if (res.errcode == 0) {
            this.$message.success("删除成功");
            this.getData();
          } else {
            this.$message.error(res.errmsg);
          }
        },
        (err) => {
          console.log(err);
        }
      );
    },
    // 状态
    changeSwitch(e, row) {
      console.log("1", e, row);
    },
    // 勾选的数据
    handleSelectionChange(val) {
      console.log("val", val);
      this.multipleSelection = val;
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getData();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getData();
    },
  },
  created() {
  },
};
</script>
<style lang="scss" scoped>
@import '../index.scss';
</style>
